package com.alix.paymoney.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.alix.paymoney.R;
import com.alix.paymoney.base.BaseActivity;
import com.alix.paymoney.utils.Loger;

import butterknife.BindView;

public class WebActivity extends BaseActivity {


    private String url = "http://dai.xiangcaolvxing.com/dai/index.html";
    @BindView(R.id.web)
     WebView web;

    public static void go(Context context, String url, String title) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected void afterCreate(@Nullable Bundle savedInstanceState) {
        url = getIntent().getStringExtra("url");
        String title = getIntent().getStringExtra("title");
        web = (WebView) findViewById(R.id.web);
        //设置可以缩放
        web.getSettings().setSupportZoom(true);
        //设置默认缩放尺寸FAR
//        web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        // 设置出现缩放工具
        web.getSettings().setBuiltInZoomControls(true);
        // 设置JavaScript可用
        web.getSettings().setJavaScriptEnabled(true);
        // 处理JavaScript对话框
//         处理各种通知和请求事件，如果不使用该句代码，将使用内置浏览器访问网页
        web.setWebChromeClient(new WebViewClient());
        web.setWebViewClient(new android.webkit.WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Loger.e(errorCode+description);

            }

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                Loger.e(error.getDescription().toString()+error.getErrorCode());


            }
        });

        web.getSettings().setUseWideViewPort(true);
        web.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        web.getSettings().setLoadWithOverviewMode(true);
        web.loadUrl(url);
    }

    class WebViewClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);

        }
    }

    @Override
    public void onBackPressed() {
        if (web.canGoBack()) {
            web.goBack();
        } else {
            super.onBackPressed();

        }
    }
}
